var table2ExcelObj = (function(){
  
  function SheetClass(opt) {
    this.ele = null;
    this.colCount = 0;
    this.opt = opt;
    this.topTable = null;
  }
  SheetClass.prototype = {
    create: function () {
      //this.colCount = 0;
      //this.topTable = null;
      var tEle = document.getElementById(this.opt.tableId);
      this.ele = tEle.cloneNode(true);
      //this.ele.style.display = "none";
      //document.body.appendChild(this.ele)
      if (this.opt.title) {
        this.createTitle(this.opt);
      }
      if (this.opt.subTitle) {
        this.createSubTitle(this.opt);
      }
      document.body.appendChild(this.ele);
      /* generate workbook object from table */
      var sht = XLSX.utils.table_to_sheet(this.ele);
      document.body.removeChild(this.ele);
      if(this.opt.colWidth) {
        sht['!cols']= [];
        if(!this.colCount){
          this.colCount = this.getColCount()
        }
        for(var n = 0; n != this.colCount; ++n){
        	sht['!cols'].push({
             wpx: this.opt.colWidth
          });
        }      
      }
      // 批准添加后 会直接盖在单元格上面 位置无法调整
//      if(this.opt.comment) {
//        var cm = this.opt.comment;
//        if(!sht[cm.cell].c){
//          sht[cm.cell].c = [];
//        }
//        sht[cm.cell].c.push({
//          a: cm.author || '',
//          t: cm.text
//        })
//        //sht[cm.cell].c.hidden = true;
//      }
      // console.log(sht.A1.c)
      return sht;
      // 开源免费版不在支持style
  //    for (var key in sht) {
  //        if (key[0] === '!') continue;
  //        sht[key].s = {
  //          Font: {
  //                    sz: 60,
  //                    bold: true
  //                  }
  //        };
  //      }  
    
    },
    
    createTitle: function(opt) {
      var row = this.createRow();
      var td = row.querySelector("td");
      td.innerHTML = opt.title;
    },
    
    createSubTitle: function(opt) {
      var row = this.createRow();
      var td = row.querySelector("td");
      td.innerHTML = opt.subTitle;          
    },
    
    createRow: function() {
      if (!this.colCount) {
        this.colCount = this.getColCount();
      }          
      if (!this.topTable) {
        this.topTable = document.createElement("table");
        var tbody = document.createElement("tbody");
        this.topTable.appendChild(tbody);
        this.ele.insertBefore(this.topTable, this.ele.firstElementChild);
      }
      var tr = document.createElement("tr");
      var td = document.createElement("td");
      td.setAttribute("colspan", this.colCount);
      tr.appendChild(td);
      this.topTable.querySelector("tbody").appendChild(tr);
      return tr;
    },
    
    getColCount: function() {
      var tr = this.ele.querySelector("table tr");
      var cols = tr.querySelectorAll("th");
      var count = 0;
      for(var i = 0; i < cols.length; i++) {
        //element中class为gutter的td默认是隐藏的,所以忽略他
        if(cols[i].classList.contains("gutter")){
          continue;
        }
        var c = parseInt(cols[i].getAttribute("colspan") || 1);
        count += c;
      }
      return count;
    }    
  }
  
  return {
    create: function(arr, opt) {
      opt = opt || {};
      if(Object.prototype.toString.call(arr) != '[object Array]'){
        //兼容之前版本,原来只有一个对象 所以文件名写在这个对象里面,现在多个对象,所以另加了一个参数.
        opt.name = arr.name;
        arr = [arr];
      }
      var wb = XLSX.utils.book_new();
      for(var i = 0; i < arr.length; i++) {
        var obj = new SheetClass(arr[i]);
        var sheet = obj.create();
        XLSX.utils.book_append_sheet(wb, sheet, arr[i].sheetName || ("sheet" + (i+1)));
      }
      /* get binary string as output */
      // var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' });
      //console.log(wbout)
      try {
        // FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), (opt.name || "data") + '.xlsx');
        XLSX.writeFile(wb, (opt.name || "data") + '.xlsx', {cellStyles:true, sheetStubs: true, WTF:1, bookSST: true});
      } catch (e){
        console.log(e)
      }
      // return wbout;       
    }
  }
  
})()
